package com.ylw.project;

import com.ylw.project.compile.ProjectSystem;
import com.ylw.project.handler.chain.impl.HandlerChainImpl;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 第二版本的入口函数
 */
public class Main {
    private static final Logger logger = Logger.getLogger(Main.class);

    public static void main(String[] args) throws InterruptedException {


//        System.setProperty("fat", "Y");

        System.out.println(System.getProperty("java.util.logging.manager"));


        /**
         * 邮件
         */
        String[] paths = new String[]{

"D:\\ideaProject\\wt_aplan_app\\src\\com\\ct10000\\store\\orderservice\\CreateUpdateOrder.java",

                "D:\\ideaProject\\wt_jobs\\src\\com\\ct10000\\business\\wt_order\\job\\JobActionJMJTOrder2WTV2.java",
                "D:\\ideaProject\\wt_jobs\\src\\com\\ct10000\\business\\wt_order\\uitls\\TMallUtil.java",
                "D:\\ideaProject\\wt_jobs\\src\\com\\ct10000\\business\\aplan\\domain\\Pr_store_order_detailDomain_exp.xml",

//                "D:\\ideaProject\\wt_tool_pro\\src\\com\\zjhcsoft\\service\\express\\JMJTRepositoryService.java",
//                "D:\\ideaProject\\wt_tool_pro\\src\\com\\zjhcsoft\\zjpr\\order\\domain\\order_express_exp.xml",
//                "D:\\ideaProject\\wt_tool_pro\\src\\com\\zjhcsoft\\zjpr\\bndict\\domain\\Jmjt_codenumber_instanceDomain_exp.xml",

//挂起报竣修复
                "D:\\ideaProject\\wt_tool_pro\\src\\com\\zjhcsoft\\service\\express\\RepositoryService.java",
                "D:\\ideaProject\\wt_tool_pro\\src\\com\\zjhcsoft\\service\\baojun\\XiaoBaiBaoJunService.java",


//
//                "D:\\ideaProject\\wt_jobs\\src\\com\\ct10000\\business\\wt_order\\job\\JobActionJMJTOrder2WT.java",
//                "D:\\ideaProject\\wt_aplan_app\\src\\com\\ct10000\\store\\orderservice\\OrderContext.java"





        };
        Main main = new Main();
        main.init(paths);
        ThreadPoolExecutor pool = ProjectSystem.getPool();
        logger.info("start time " + new Date());
        for (int i = 0; i < paths.length; i++) {
            pool.execute(new HandlerChainImpl(paths[i]));
        }


//        while(pool.isShutdown()){
//            logger.info("pool getActiveCount " + pool.getActiveCount());
//            logger.info("pool getCompletedTaskCount " + pool.getCompletedTaskCount());
//            logger.info("pool allowsCoreThreadTimeOut " + pool.allowsCoreThreadTimeOut());
//
////            logger.info("pool "+pool.getKeepAliveTime());
//
//            logger.info("pool size showtime");
//            logger.info("pool getCorePoolSize " + pool.getCorePoolSize());
//            logger.info("pool getLargestPoolSize " + pool.getLargestPoolSize());
//            logger.info("pool getPoolSize " + pool.getPoolSize());
//            logger.info("pool getMaximumPoolSize " + pool.getMaximumPoolSize());
//            Thread.sleep(500);
//        }

//        线程和main线程是并发的，所以下面的信息不是在所有线程执行完了之后运行的
//        这里不能直接调用pool.shutdown因为编译线程在运行时加入pool里面的
        while (pool.isTerminated()) {
            logger.info("pool getActiveCount " + pool.getActiveCount());
            logger.info("pool getCompletedTaskCount " + pool.getCompletedTaskCount());
            logger.info("pool allowsCoreThreadTimeOut " + pool.allowsCoreThreadTimeOut());

//            logger.info("pool "+pool.getKeepAliveTime());

            logger.info("pool size showtime");
            logger.info("pool getCorePoolSize " + pool.getCorePoolSize());
            logger.info("pool getLargestPoolSize " + pool.getLargestPoolSize());
            logger.info("pool getPoolSize " + pool.getPoolSize());
            logger.info("pool getMaximumPoolSize " + pool.getMaximumPoolSize());

            pool.shutdown();//会处理完队列里面的任务
            Thread.sleep(1000);
        }

        //不知道为啥没有进去两个while循环，就直接system.exit了
        logger.info("end time" + new Date());

    }

    private void init(String[] paths) {
        Map<String, Integer> map = new HashMap();
        for (int i = 0; i < paths.length; i++) {
            if (!paths[i].endsWith(".java")) {
                continue;
            }
            String projectName = Tools.getProjectName(paths[i]);
            if (map.containsKey(projectName)) {
                Integer t = map.get(projectName);
                t++;
//                System.out.println(projectName);
            } else {
                map.put(projectName, 1);
            }
        }
        for (String key : map.keySet()) {
            ProjectSystem.addCountJava(key, map.get(key));
        }

    }
}
